import {
  Form
} from '@tarojs/components'
import {
  request,
  requestGet,
  requestPost,
} from '../util/request'

import {
  before,
  once
} from '../util/util'

const base_url = 'http://182.92.196.159:8090/'


const obbj = {
  data: {},
  setData: function (obj) {
    console.log(obj)
    this.data = Object.assign(this.data, obj)
  },
  getData: function (key) {
    let {
      data
    } = this
    console.log(data[key])
    return data.hasOwnProperty(key) ? data[key] : void 0
  },
}

const setstore = (obj) => obbj.setData.call(obbj, obj)
const getstore = (key) => obbj.getData.call(obbj, key)

const funParam = (obj, hasHeader = true) => {
  let newObj = Object.assign(obj, {
    url: base_url + obj.url
  })
  if (hasHeader) {
    let key = getstore('session'),
      sessionHeader = key ? {
        'Session-Token': key
      } : {},
      header = newObj.hasOwnProperty('header') ? newObj.header : {};
    newObj.header = Object.assign(header, sessionHeader)
  }
  return newObj
}


const login = (code) => {
  let funParamObj = funParam({
    url: 'api/project/front/user/login',
    data: {
      code
    }
  }, false)
  return requestPost(funParamObj)
}

const setToken = (code) => login(code).then(res => {
  let item = res.result.item
  setstore({
    session: item
  })
  getstore('session')
  return item
})


const userInfo = () => {
  let funParamObj = funParam({
    url: 'api/project/front/user/info'
  })
  return requestGet(funParamObj)
}

const activeFilePage = (data) => {
  let funParamObj = funParam({
    url: 'api/project/front/activeFile/page'
  }, data)
  return requestPost(funParamObj)
}

const activeFileCreate = (data) => {
  let funParamObj = funParam({
    url: 'api/project/front/activeFile/create'
  }, data)
  return requestPost(funParamObj)
}

const fileUploadFile = (data) => {
  let funParamObj = funParam({
    url: 'project/front/file/uploadFile'
  }, data)
  return requestPost(funParamObj)
}

const activeParticipantCancel = (data) => {
  let funParamObj = funParam({
    url: 'api/project/front/activeParticipant/cancel'
  }, data)
  return requestPost(funParamObj)
}

// const activeParticipantCancel = (data)=>{
//   let funParamObj = funParam({url:'api/project/front/activeParticipant/cancel'},data)
//   return requestPost(funParamObj)
// }

const activeParticipantCreate = (data) => {
  let funParamObj = funParam({
    url: 'api/project/front/activeParticipant/create'
  }, data)
  return requestPost(funParamObj)
}

const activityDetail = (id) => {
  let funParamObj = funParam({
    url: 'api/project/front/activity/detail',
    header: {
      id
    }
  })
  return requestGet(funParamObj)
}

const activityPage = (data) => {
  let funParamObj = funParam({
    url: 'api/project/front/activity/page'
  }, data)
  return requestPost(funParamObj)
}

const activityList = (data) => {
  let funParamObj = funParam({
    url: 'api/project/front/activity/list'
  }, data)
  return requestPost(funParamObj)
}

export {
  setstore,
  getstore,
  login,
  setToken,
  userInfo,
  activeFilePage,
  activeFileCreate,
  fileUploadFile,
  activeParticipantCancel,
  activeParticipantCreate,
  activityDetail,
  activityPage,
  activityList
}
